package me.xu.wifiautosync;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

public class AutoSyncReceiver extends BroadcastReceiver
{
	private static final String TAG = "autosync";
	@Override
	public void onReceive(Context context, Intent intent)
	{
		boolean sync = false;

		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
		{
			WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			if (wifiInfo != null)
			{
				String ssid = wifiInfo.getSSID();
				Log.i(TAG, "ssid:" + ssid);
				if ("\"T2\"".equals(ssid) || "\"Huawei-Employee\"".equals(ssid))
				{
					sync = true;
				}
			}
		}
		
		ContentResolver.setMasterSyncAutomatically(sync);		
	}
}
